const incID = require("../functions/incID");
const Plays = require("../models/Plays")

const addPlay = async (req, res) => {
    try {
        const playExist = await Plays.findOne({ id_store: req.body.id_store, id_drama: req.body.id_drama });
        if (playExist) {
            return res.status(400).json({ error_msg: '店铺内已有该剧本' });
        }
        req.body.id_play = await incID("play");

        const playsadd = await Plays(req.body).save()

        return res.status(200).json({ success: playsadd });

    } catch (err) {
        return res.status(400).json({ error_msg: err.message });
    }
}

const removePlay = async (req, res) => {
    try {
        const playExist = await Plays.findById(req.body._id);
        if (playExist) {
            const playremoved = await playExist.remove()
            return res.status(200).json({ success: playremoved });
        }
        else {
            return res.status(400).json({ error_msg: "店铺内不存在该剧本" });
        }

    } catch (err) {
        return res.status(400).json({ error_msg: err.message });
    }
}

const updatePlay = async (req, res) => {
    try {
        const playExist = await Plays.findById(req.body._id);
        if (playExist) {
            const savequery = req.body
            delete savequery._id
            for (item in savequery) {
                if (typeof (savequery[item]) == "object") {
                    playExist[item] = { ...playExist[item], ...savequery[item] }
                } else {
                    playExist[item] = savequery[item]
                }
            }
            const playupdated = await playExist.save()
            return res.status(200).json({ success: playupdated });
        }
        else {
            return res.status(400).json({ error_msg: "店铺内不存在该剧本" });
        }
    } catch (err) {
        return res.status(400).json({ error_msg: err.message });
    }
}

const getPlay = async (req, res) => {
    try {
        const foundquery = req.query
        for (item in foundquery) {
            foundquery[item] = eval("/" + foundquery[item] + "/")
        }


        const a = await Plays.aggregate([
            {
                $lookup: {
                    from: "Stores",
                    localField: "id_store",
                    foreignField: "_id",
                    as: "Stores",
                }
                
            },{
                $lookup: {
                    from: "Dramas",
                    localField: "id_drama",
                    foreignField: "_id",
                    as: "Dramas",
                }
                
            },
            {
                $match: foundquery
            }

        ])
        
        // console.log(foundquery)
        // const playfound = await Plays.find(foundquery).populate("id_store id_drama")
        // if (!playfound) {
        //     return res.status(200).json({ error_msg: "店铺内不存在该剧本" });
        // }
        return res.status(200).json({ success: a });
    }
    catch (err) {
        return res.status(400).json({ error_msg: err.message });
    }
}



module.exports = { addPlay, removePlay, updatePlay, getPlay }